package com.huahang.fashionpress.controller;

import com.huahang.fashionpress.form.FormAdmin;
import com.huahang.fashionpress.mapper.NickMapper;
import com.huahang.fashionpress.model.Admin;
import com.huahang.fashionpress.model.Nick;
import com.huahang.fashionpress.service.AdminService;
import com.huahang.fashionpress.util.Md5Util;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @Author: 阿俊哥 李梦阳
 * @Date: 2019/8/25 18:56
 * @Version 1.0
 */
@Controller
@Slf4j
@RequestMapping("/admin")
public class AdminController {

    /**
     * 网站主页入口
     * @return
     */
    @RequestMapping("/index")
    public ModelAndView index(){
        return new ModelAndView("/admin/index");
    }

    @Resource
    AdminService adminService;
    @Resource
    NickMapper nickMapper;
    //显示所有管理员信息
    @RequestMapping(value = "/list")
    public ModelAndView linklistAdmin(Map<String,Object> map){
        List<FormAdmin> adminlist= adminService.SelectFAdminlist();
        map.put("adminlist",adminlist);
        return new ModelAndView("admin/admin/list",map);
    }
    @RequestMapping(value = "/selectByadminId/{adminId}")
    public ModelAndView SelectAdminByadminId(@PathVariable int adminId, Map<String,Object> map){
        List<FormAdmin> adminlist =new ArrayList<>();
        FormAdmin formAdmin = adminService.SelectFAdminByadminId(adminId);
        adminlist.add(formAdmin);
        map.put("adminlist",adminlist);
        return new ModelAndView("admin/admin/list",map);
    }
    @RequestMapping(value = "/selectBynickName/{nickName}")
    public ModelAndView SelectAdminBynickName(@PathVariable String nickName,Map<String,Object> map){
        List<FormAdmin> adminlist = adminService.SelectFAdminBynickName(nickName);
        map.put("adminlist",adminlist);
        return new ModelAndView("admin/admin/list",map);
    }
    @RequestMapping(value = "/add")
    public ModelAndView linkaddAdmin(Map<String,Object> map){
        List<Nick> nickList = nickMapper.selectAllNicks();
        map.put("nickList",nickList);
        return new ModelAndView("admin/admin/add",map);
    }
    //更改管理员状态
    @RequestMapping(value="/updateStatus")
    public String updateStatus(int adminId, Model model){
        Admin admin = adminService.selectAdminById(adminId);
        adminService.updateAdminStatus(admin);
        model.addAttribute("msg", "更改管理员状态成功！");
        model.addAttribute("url", "/fashionpress/admin/list");
        return "admin/common/success";
    }
    //获得需要更改的管理员信息
    @RequestMapping(value = "/update",method = RequestMethod.GET)
    public ModelAndView getupdateif(Map<String,Object> map,int adminId){
        FormAdmin formAdmin = adminService.SelectFAdminByadminId(adminId);
        List<Nick> nickList = nickMapper.selectAllNicks();
        map.put("nickList",nickList);
        map.put("formAdmin",formAdmin);
        return new ModelAndView("admin/admin/add",map);
    }
    @RequestMapping(value = "/update",method = RequestMethod.POST)
    public String updateadmin(FormAdmin formAdmin, Model model){
        Admin admin = new Admin();
        if(formAdmin.getAdminId()==null){
            admin.setAdminStatus(formAdmin.getAdminStatus());
            admin.setAdminPassword(Md5Util.getMd5String(formAdmin.getAdminPassword()));
            admin.setAdminPhone(formAdmin.getAdminPhone());
            admin.setAdminUsername(formAdmin.getAdminUsername());
            admin.setNickId(formAdmin.getNickId());
            adminService.addAdmin(admin);
            model.addAttribute("msg", "添加管理员信息成功！");
        }else{
            admin.setAdminId(formAdmin.getAdminId());
            admin.setAdminStatus(formAdmin.getAdminStatus());
            admin.setAdminPassword(formAdmin.getAdminPassword());
            admin.setAdminPhone(formAdmin.getAdminPhone());
            admin.setAdminUsername(formAdmin.getAdminUsername());
            admin.setNickId(formAdmin.getNickId());
            adminService.updateAdmin(admin);
            model.addAttribute("msg", "修改管理员信息成功！");
        }
        model.addAttribute("url", "/fashionpress/admin/list");
        return "admin/common/success";
    }
}
